package cetcbigdata.da.personal_service.zhejiang;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.exception.XpathSyntaxErrorException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpiderZheJiang extends Base {
    private static String baseRegion = "浙江省";

    private static String baseModule = "个人服务";

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        this.parseListPage(recordSender, fieldsList);
    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private void parseListPage(RecordSender recordSender, List<Object> fieldsList){
        try {
            for(JSONObject regionItem : getRegionList()){
                List<JSONObject> topicList = getTopic();
                for(JSONObject topicItem : topicList){
                    String module = baseModule + "-" + topicItem.getString("name");
                    String region = baseRegion;
                    getPageList(topicItem.getString("id"), topicItem.getString("name"), region, module, recordSender, fieldsList, regionItem);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 获取主题信息： 生育收养、 户籍办理 等
     * @return List<JSONObject>
     */
    private static void getPageList(String id, String name, String region, String module, RecordSender recordSender, List<Object> fieldsList,
                                    JSONObject regionItem){
        // 当没有数据返回时，网站中会出现 div[@id='nodata'] 元素，则停止翻页
        int pageno = 1;
        while (true){
            String url = "https://www.zjzwfw.gov.cn/zjservice/home/person/getPersonalItemList.do";
            JSONObject parmas = new JSONObject();
            parmas.put("1", 1);
            parmas.put("webid", regionItem.getString("id"));
            parmas.put("pagesize", 20);
            parmas.put("pageno", pageno);
            parmas.put("type", 1);
            parmas.put("classify", 3);
            parmas.put("subHeadId", id);
            parmas.put("name", name);
            parmas.put("word", "");
            parmas.put("impleType", "");
            parmas.put("serviceType", "");
            parmas.put("isnoline", "");
            parmas.put("plc", "");
            parmas.put("tongBanFlag", "");
            JSONObject headers = UtilZheJiang.getHeaders();
            String listResponse =  getReq(url, headers, parmas);
            String regex = "var _0x4818=([\\s\\S]*?);";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(listResponse);
            // 判断是否需要更新cookie参数
            if(matcher.find()){
                // 需要更新
                regex = "var arg1=([\\s\\S]*?);";
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(listResponse);
                String acw_sc__v2 = null;
                if (matcher.find()) {
                    String arg1 = matcher.group();
                    arg1 = arg1.replace("var arg1='", "");
                    arg1 = arg1.replace("';", "");
                    acw_sc__v2 = Js.getEncode(arg1);
                }
                // 更新cookie
                headers.put("Cookie", "acw_sc__v2=" + acw_sc__v2);
                // 更新cookie后，重新请求
                listResponse =  getReq(url, headers, parmas);
            }
            JXDocument jxDocument = new JXDocument(listResponse);
            if(jxDocument.sel("//div[@id='nodata']").size() == 1){
                break;
            }
            parseListPage(url, listResponse, headers, parmas, region, module, recordSender, fieldsList, regionItem );
            pageno ++;
        }
    }

    private static void parseListPage(String listUrl, String listResponse , JSONObject headers, JSONObject parmas, String region, String module,
                                                      RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem){
        JXDocument jxDocument = new JXDocument(listResponse);
        List<Object> firtTitle = jxDocument.sel("//div[@class='item_title']");
        for (Object o : firtTitle){
            // 声明定义列表页每一项数据
            JSONObject content = new JSONObject();
            String firstTitle  = ((Element) o).child(0).text();
            content.put("名称", firstTitle);

            Elements subTitleList = ((Element) o).nextElementSibling().children();
            Elements childrenList = null;
            // 判断时候含有子标题, 含有子标题
            if(subTitleList.get(0).attr("class").equals("item_threeTitle")){
                String subTitle = ((Element) o).nextElementSibling().child(0).text();
                content.put("子标题", subTitle);
                childrenList = ((Element) o).nextElementSibling().child(1).children();
            }else {
                childrenList = subTitleList;
            }

            List<JSONObject> xuanxiang = new ArrayList<>();
            List<String> detailUrls = new ArrayList<>();
            List<String> detailResponses = new ArrayList<>();
            for(Element subTitleElement : childrenList){
                JSONObject xuanxiangItem = new JSONObject();
                String subSubTitle  = subTitleElement.child(0).text();
                String localInnerCode  = subTitleElement.child(0).attr("onclick");
                localInnerCode = localInnerCode.replace("bszn('", "").replace("')", "");
                String detailUrl = "https://www.zjzwfw.gov.cn/zjservice/item/detail/index.do?localInnerCode=" + localInnerCode;
                String detailResponse = parseDetaiPage(detailUrl, headers, parmas);
                xuanxiangItem.put("名称", subSubTitle);
                xuanxiangItem.put("信息", getDetailPage(detailResponse));
                xuanxiang.add(xuanxiangItem);
                detailUrls.add(detailUrl);
                detailResponses.add(detailResponse);
            }
            content.put("选项", xuanxiang);

            // datax 处理数据
            processItem(
                    processResult(
                            listUrl, listResponse, String.join(",", detailUrls), String.join(",", detailResponses),
                            content, region + "-" + regionItem.getString("name"), module),
                    recordSender,
                    fieldsList
            );
        }
    }

    private static String parseDetaiPage(String url, JSONObject headers, JSONObject params){
        String response = getReq(url, headers, params);
        String regex = "var _0x4818=([\\s\\S]*?);";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(response);
        // 判断是否需要更新cookie参数
        if(matcher.find()){
            // 需要更新
            regex = "var arg1=([\\s\\S]*?);";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(response);
            String acw_sc__v2 = null;
            if (matcher.find()) {
                String arg1 = matcher.group();
                arg1 = arg1.replace("var arg1='", "");
                arg1 = arg1.replace("';", "");
                acw_sc__v2 = Js.getEncode(arg1);
            }
            // 更新cookie
            headers.put("Cookie", "acw_sc__v2=" + acw_sc__v2);
        }else {
            return response;
        }
        return getReq(url, headers, params);
    }

    private static JSONObject getDetailPage(String html){
        JXDocument jxDocument = new JXDocument(html);
        JSONObject content = new JSONObject();
        content.put("信息", UtilZheJiang.geExtraInfo(jxDocument));
        content.put("基本信息", UtilZheJiang.getBaseInfo(jxDocument));
        content.put("线下办事点", UtilZheJiang.getOffLinePlace(jxDocument));
        content.put("申请条件", UtilZheJiang.getApplyConditionInfo(jxDocument));
        content.put("申报材料", UtilZheJiang.getApplicationMaterialsInfo(jxDocument));
        content.put("办理流程", UtilZheJiang.getProcessFlowTableInfo(jxDocument));
        content.put("设定依据", UtilZheJiang.getSettingBasisInfo(jxDocument));
        content.put("收费标准", UtilZheJiang.getChargeStandardInfo(jxDocument));
        content.put("常见问题", UtilZheJiang.getQuestionInfo(jxDocument));
        return content;
    }


    /***
     * 获取主题信息： 生育收养、 户籍办理 等
     * @return List<JSONObject>
     */
    private static List<JSONObject> getTopic(){
        String url = "https://www.zjzwfw.gov.cn/zjservice/home/person/index.do?subHeadId=005&subHeadName=%25E7%2594%259F%25E8%2582%25B2%25E6%2594%25B6%25E5%2585%25BB&webId=1&classify=3";
        String response = getReq(url, UtilZheJiang.getHeaders(), UtilZheJiang.getParams());
        List<JSONObject> out = new ArrayList<>();
        JXDocument jxDocument = new JXDocument(response);
        List<Object> rs = jxDocument.sel("//ul[@class='topicN_list clearfix']/li/a");
        for (Object o : rs){
            JSONObject topicJson = new JSONObject();
            String id  = ((Element) o).attr("id");
            String name = ((Element) o).attr("title");
            topicJson.put("name", name);
            topicJson.put("id", id);
            out.add(topicJson);
        }
        return out;
    }


    private static List<JSONObject> getRegionList(){
        String url = "https://zjjcmspublic.oss-cn-hangzhou-zwynet-d01-a.internet.cloud.zj.gov.cn/jcms_files/jcms1/web1/site/script/zjzwfw-2019/new_json2.js";
        JSONObject headers = new JSONObject();
        headers.put("Host", "zjjcmspublic.oss-cn-hangzhou-zwynet-d01-a.internet.cloud.zj.gov.cn");
        headers.put("Referer", "https://www.zjzwfw.gov.cn/");
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        String response = null;
        try {
            String responseOrg = getReq(url, headers, UtilZheJiang.getParams());
            response = new String(responseOrg.getBytes("iso-8859-1"), "utf-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        response = response.replace("var jsonwebsite=", "");
        JSONArray dataList =  ( (JSONObject) JSONObject.parseArray(response).get(0)).getJSONArray("childs");
        List<JSONObject> out = new ArrayList<>();
        // 遍历市
        for(Object o : dataList){
            JSONObject shiItem = (JSONObject) o;
            // 遍历区
            JSONArray childs = shiItem.getJSONArray("childs");
            for(Object oo : childs){
                JSONObject quItem = (JSONObject) oo;
                JSONObject outItem = new JSONObject();
                outItem.put("id", quItem.getString("id"));
                outItem.put("name", shiItem.getString("name") + "-" + quItem.getString("name"));
                out.add(outItem);
            }
        }
        return out;
    }

    public static void main(String[] args) {
        SpiderZheJiang spiderZheJiang = new SpiderZheJiang();
        spiderZheJiang.getData(null, new ArrayList<>());
    }
}
